import 'package:flutter/material.dart';
import 'package:example/base/custom_widget.dart';
import 'package:example/utils/common.dart';
import 'package:example/config/color_config.dart';
import 'package:get/get.dart';

class PrimaryBtn extends CustomWidget<PrimaryBtnController> {
  final Function? onTap;
  final EdgeInsets? margin;
  final EdgeInsets? padding;
  final double? height;
  final double? width;
  final double? minWidth;
  final double? minHeight;
  final Color? splashColor;
  final BorderRadius? borderRadius;
  final String text;
  final double? fontSize;
  final FontWeight? fontWeight;
  final Color? textColor;
  final TextStyle? textStyle;
  final Color? textDisabledColor;
  final Color? background;
  final Color? backgroundDisabled;
  final LinearGradient? backgroundGradient;
  final LinearGradient? backgroundDisabledGradient;
  final BorderSide? border;
  final double? elevation;
  final Color? shadowColor;
  final Alignment? alignment;

  PrimaryBtn({super.key,
    PrimaryBtnController? controller,
    bool isEnabled = true,
    bool isGradient = false,
    this.onTap,
    this.border,
    this.margin,
    this.padding = EdgeInsets.zero,
    this.height,
    this.width,
    this.minWidth,
    this.minHeight,
    this.splashColor = Colors.black,
    this.borderRadius,
    this.text = "",
    this.textColor = ColorConfig.text,
    this.textDisabledColor = ColorConfig.text_disabled,
    this.fontSize,
    this.fontWeight,
    this.textStyle,
    this.alignment,
    this.background,
    this.backgroundDisabled = ColorConfig.FFC7D7FA,
    this.backgroundGradient = const LinearGradient(
      colors: [],
    ),
    this.backgroundDisabledGradient = const LinearGradient(
      colors: [ColorConfig.FFC7D7FA, ColorConfig.FFC7D7FA],
    ),
    this.elevation,
    this.shadowColor,
  }) : super(PrimaryBtnController(isEnabled: isEnabled, isGradient: isGradient),
            externalController: controller);

  @override
  Widget initView(BuildContext context, PrimaryBtnController controller) {
    Function fun = throttle(onTap);
    bool isEnabled = controller.isEnabled;
    bool isGradient = controller.isGradient;
    return Container(
      margin: margin,
      height: height,
      width: width,
      decoration: BoxDecoration(
        color: isGradient
            ? null
            : isEnabled
                ? background
                : backgroundDisabled,
        gradient: !isGradient
            ? null
            : isEnabled
                ? backgroundGradient
                : backgroundDisabledGradient,
        borderRadius: borderRadius,
      ),
      child: TextButton(
        style: TextButton.styleFrom(
          alignment: alignment,
          elevation: isEnabled ? elevation : 0,
          shadowColor: isEnabled ? shadowColor : Colors.transparent,
          tapTargetSize: MaterialTapTargetSize.shrinkWrap,
          minimumSize: Size(minWidth ?? 0, minHeight ?? 0),
          padding: padding,
          foregroundColor: splashColor,
          side: border,
          shape: RoundedRectangleBorder(
            borderRadius: borderRadius ?? BorderRadius.circular(0),
          ),
        ),
        onPressed: isEnabled ? () => fun.call() : null,
        child: Text(
          controller.text == null ? text : controller.text!,
          style: textStyle ??
              TextStyle(
                fontWeight: fontWeight,
                fontSize: fontSize,
                color: isEnabled ? textColor : textDisabledColor,
              ),
        ),
      ),
    );
  }
}

class PrimaryBtnController extends GetxController {
  bool isEnabled;
  bool isGradient;
  String? text;

  PrimaryBtnController(
      {this.text, this.isGradient = false, this.isEnabled = true});

  setText(String text) {
    this.text = text;
    update();
  }
}
